package cn.muqingfeng.bbs.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import cn.muqingfeng.bbs.R;
import cn.muqingfeng.bbs.constants.Constants;
import cn.muqingfeng.bbs.ui.AvatarPickerActivity;

public class AvatarPickerPopup extends PopupWindow implements View.OnClickListener {

	private View mContentView;
	private View mPickFromLocalView;
	private View mPickFromCameraView;
	private Activity mActivity;
	private boolean mCrop = true;

	public AvatarPickerPopup(Activity activity) {
		super(activity);
		mActivity = activity;
		LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mContentView = inflater.inflate(R.layout.avatar_pick_popup, null);
		mPickFromLocalView = mContentView.findViewById(R.id.tvButtonLocalPhoto);
		mPickFromCameraView = mContentView.findViewById(R.id.tvButtonTakePhoto);
		mPickFromLocalView.setOnClickListener(this);
		mPickFromCameraView.setOnClickListener(this);
		mContentView.setOnClickListener(this);
		mContentView.findViewById(R.id.cancel).setOnClickListener(this);
		setContentView(mContentView);
		setHeight(FrameLayout.LayoutParams.MATCH_PARENT);
		setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
		setFocusable(true);
		setAnimationStyle(R.style.popup_style);
		ColorDrawable drawable = new ColorDrawable(activity.getResources().getColor(R.color.translucence));
		setBackgroundDrawable(drawable);
	}

	public void setCrop(boolean mCrop) {
		this.mCrop = mCrop;
	}
	
	public void dismissPopup() {
		Animation animation = AnimationUtils.loadAnimation(mActivity, R.anim.to_bottom);
		mContentView.clearAnimation();
		animation.setFillAfter(true);
		animation.setAnimationListener(new DismissAnimationListener() {

			@Override
			public void onAnimationEnd(Animation animation) {
				super.onAnimationEnd(animation);
				new Handler().post(new Runnable() {

					@Override
					public void run() {
						AvatarPickerPopup.super.dismiss();
					}
				});
			}
		});
		mContentView.setAnimation(animation);
	}

	@Override
	public void dismiss() {
		dismissPopup();
	}

	@Override
	public void showAsDropDown(View anchor, int xoff, int yoff) {
		super.showAsDropDown(anchor, xoff, yoff);
		startShowAnimation();
	}

	@Override
	public void showAtLocation(View parent, int gravity, int x, int y) {
		super.showAtLocation(parent, gravity, x, y);
		startShowAnimation();
	}

	private void startShowAnimation() {
		mContentView.clearAnimation();
		Animation animation = AnimationUtils.loadAnimation(mActivity, R.anim.from_bottom);
		animation.setFillAfter(true);
		mContentView.setAnimation(animation);
	}

	class DismissAnimationListener implements Animation.AnimationListener {

		private int position = -1;

		public DismissAnimationListener() {
		}

		public DismissAnimationListener(int position) {
			this.position = position;
		}

		public int getPosition() {
			return position;
		}

		@Override
		public void onAnimationStart(Animation animation) {

		}

		@Override
		public void onAnimationEnd(Animation animation) {

		}

		@Override
		public void onAnimationRepeat(Animation animation) {

		}

	}

	@Override
	public void onClick(View v) {
		dismiss();

		if (v == mPickFromLocalView) {
			Intent intent = new Intent(mActivity, AvatarPickerActivity.class);
			intent.putExtra("PickWay", "PICK");
			intent.putExtra(AvatarPickerActivity.EXTRA_CROP, mCrop);
			mActivity.startActivityForResult(intent, Constants.REQUEST_CODE_PICK_PHOTO);
		} else if (v == mPickFromCameraView) {
			Intent intent = new Intent(mActivity, AvatarPickerActivity.class);
			intent.putExtra("PickWay", "TAKE");
			intent.putExtra(AvatarPickerActivity.EXTRA_CROP, mCrop);
			mActivity.startActivityForResult(intent, Constants.REQUEST_CODE_TAKE_PHOTO);
		} else if (v == mContentView || v.getId() == R.id.cancel) {
			dismiss();
		}
	}
}